<template>
  <div style="padding:20px">
    <h3>新增文创产品</h3>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" style="width:80%" label-width="100px">
      <el-form-item label="文创名" prop="name">
        <el-input v-model="form.name" placeholder="请输入文创名"></el-input>
      </el-form-item>

      <el-form-item label="文创介绍" prop="description">
        <el-input v-model="form.description" type="textarea" style="width:400px" placeholder="请输入文创介绍内容"></el-input>
      </el-form-item>

      <el-form-item label="文创图" prop="cover">
        <el-input v-model="form.cover" placeholder="请输入图片链接"></el-input>
      </el-form-item>

      <el-form-item label="藏品分类" prop="category">
<!--        <el-input v-model="form.category" placeholder="请输入藏品类别"></el-input>-->
        <el-cascader
            :props="{ value: 'name',label:'name' }"
            v-model="form.categories"
            :options="categories"
            @change="selCategory(form.categories)" placeholder="请选择产品分类"></el-cascader>
      </el-form-item>

      <el-form-item label="藏品类别ID" prop="categoryId">
        <el-input v-model="form.categoryId" placeholder="请输入产品类别ID" disabled></el-input>
      </el-form-item>

      <el-form-item label="藏品类别名称" prop="categoryName">
        <el-input v-model="form.categoryName" placeholder="请输入产品类别名称" disabled></el-input>
      </el-form-item>

      <el-form-item label="产品价格" prop="price">
        <el-input v-model="form.price" placeholder="请输入产品价格"></el-input>
      </el-form-item>

      <el-form-item label="产品销量" prop="sales">
        <el-input v-model="form.sales" placeholder="请输入产品销量"></el-input>
      </el-form-item>

      <el-form-item label="库存量" prop="inventory">
        <el-input v-model="form.inventory" placeholder="请输入产品库存量"></el-input>
      </el-form-item>

    </el-form>

    <div style="text-align:center;margin-top:30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
      <!--            <el-button type="danger">返回列表</el-button>-->
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "AddProducts",
  data(){
    return{
      form:{},
      //categories从分类中去请求
      categories:[],
      rules: {
        name: [
          {required: true, message: '请输入产品名称', trigger: 'blur'},
        ],
        description: [
          {required: true, message: '请输入产品介绍', trigger: 'blur'},
        ],
        price: [
          {required: true, message: '请输入产品价格', trigger: 'blur'},
        ],
        inventory: [
          {required: true, message: '请输入产品库存', trigger: 'blur'},
        ],
        cover: [
          {required: true, message: '请输入产品图', trigger: 'blur'},
        ],
        categories: [
          {required: true, message: '请输入类别名', trigger: 'blur'},
        ],
        sales:[
          {required: true, message: '请输入产品销量', trigger: 'blur'},
        ]
      }
    }
  },
  created() {
    request.get('/producttype/tree').then(res => {
      this.categories = res.data
      // console.log("categories内容是",this.categories)
    })
  },
  methods: {
    save(){
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          //  save方法用于提交数据
          request.post('/products/save',this.form).then(res => {
            if (res.code === '200'){
              this.$notify.success('新增成功')
              this.form = {}
              //  新增成功后resetFields清空输入框内数据
              this.$refs['ruleForm'].resetFields();
              // console.log("框中内容是",this.$refs['ruleForm'])
            } else{
              this.$notify.error(res.msg)
              // console.log("失败的内容是",this.categories)
            }
          })
        }
      })
    },
    selCategory(data){
        //data[1]是该类别名称,还得匹配到在producttype里的id 先匹配data[0]
      // const products = this.categories[2].children.find(v => v.name === data[0])
      this.categories.forEach((item)=>{
        if(data[0] === item.name){
          item.children.forEach((pItem)=>{
            if(pItem.name === data[1]){
               this.form.categoryName = pItem.name
              this.form.categoryId = pItem.id
            }
          })
        }
      })
      // console.log("当前children的id是",products)
    }

  }
}
</script>

<style scoped>

</style>